﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SuperStarProject.Models;
using System.Data.SqlClient;
using System.Data;
namespace SuperStarProject.DAO
{
    public static class ProductDAO
    {
        public static List<ProductModel> ShowAllProducts()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("sp_select_SanPham", db);
            return ConvertDataReaderToListProduct(rdr);
        }
        public static List<ProductModel> ShowAllRandomProducts()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("sp_select_SanPham", db);
            return ConvertDataReaderToListProduct(rdr);
        }
        public static List<ProductModel> ShowAllProductForStatic()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("xemtatca_muanhieu", db);
            List<ProductModel> lstproduct = new List<ProductModel>();
            while (rdr.Read())
            {
                string masp = rdr["masp"].ToString();
                var item = ProductDAO.ViewDetails(masp);
                item.SoLuong = int.Parse(rdr["SoLuongNguoiDaMua"].ToString());
                lstproduct.Add(item);
            }
            return lstproduct;
        }
        public static List<ProductModel> ShowAllHotProducts()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("xem_muanhieu", db);
            List<ProductModel> lstproduct = new List<ProductModel>();
            while (rdr.Read())
            {
                string masp = rdr["masp"].ToString();
                var temp = ProductDAO.ViewDetails(masp);
                if ((temp.TGBatDauBan <= DateTime.Now) && (temp.TGKetThucBan >= DateTime.Now))
                    lstproduct.Add(temp);
            }
            return lstproduct;
        }
        public static List<ProductModel> ShowAllNewProducts()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("xem_moinhat", db);
            return ConvertDataReaderToListProduct(rdr);
        }
        public static List<ProductModel> ShowAllEndedProducts()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("xem_daketthuc", db);
            return ConvertDataReaderToListProduct(rdr);
        }
        public static bool CreateProduct(ProductModel model)
        {
            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();

            lstParameter.Add(new SqlParameter("@masp", (object)model.MaSP));
            lstParameter.Add(new SqlParameter("@manhacungcap", (object)model.MaNhaCungCap));
            lstParameter.Add(new SqlParameter("@manv", (object)model.MaNhanVien));
            lstParameter.Add(new SqlParameter("@tensp", (object)model.TenSP));
            lstParameter.Add(new SqlParameter("@diemthuong", (object)model.DiemThuong));
            lstParameter.Add(new SqlParameter("@giatri", (object)model.GiaTriTien));
            lstParameter.Add(new SqlParameter("@beginsale", (object)model.TGBatDauBan));
            lstParameter.Add(new SqlParameter("@endsale", (object)model.TGKetThucBan));
            lstParameter.Add(new SqlParameter("@beginsend", (object)model.TGBatDauNhan));
            lstParameter.Add(new SqlParameter("@endsend", (object)model.TGKetThucNhan));
            lstParameter.Add(new SqlParameter("@soluong", (object)model.SoLuong));
            lstParameter.Add(new SqlParameter("@hinh", (object)model.HinhAnh));
            int result = DataProvider.RunStoredProcParams_NoRead("sp_themsp", db, lstParameter);
            if (result == 0)
                return true;
            return false;
        }
        public static ProductModel ViewDetails(string productId)
        {
            ProductModel prod = new ProductModel();
            SqlParameter para_productId = new SqlParameter("@ma", (object)productId);

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(para_productId);

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_SanPham_dk", db,lstParameter);
            try
            {
                return ConvertDataReaderToListProduct(rdr).First();
            }
            catch(Exception ex)
            {
                throw ex;
            }
                
        }
        static List<ProductModel> ConvertDataReaderToListProduct(SqlDataReader rdr)
        {
            List<ProductModel> lstProduct = new List<ProductModel>();
            while (rdr.Read())
            {
                ProductModel prod = new ProductModel();
                prod.MaSP = rdr["MaSP"].ToString();
                prod.MaNhaCungCap = (string)rdr["MaNhaCungCap"];
                prod.MaNhanVien = (string)rdr["MaNV"];
                prod.TenSP = (string)rdr["TenSP"];
                prod.HinhAnh = (string)rdr["HinhAnh"];
                prod.DiemThuong = (int)rdr["DiemThuong"];
                prod.GiaTriTien = (int)rdr["GiaTriTien"];
                prod.TGBatDauBan = (DateTime)rdr["TGBatDauBan"];
                prod.TGKetThucBan = (DateTime)rdr["TGKetThucBan"];
                prod.TGBatDauNhan = (DateTime)rdr["TGBatDauNhan"];
                prod.TGKetThucNhan = (DateTime)rdr["TGKetThucNhan"];
                prod.SoLuong = GetSoLuongToiDaCoThe(prod.MaSP);
                lstProduct.Add(prod);
            }
            return lstProduct;
        }
        public static int GetSoLuongToiDaCoThe(string productId)
        {
            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(new SqlParameter("@ma", (object)productId));
            SqlParameter soluongcon = new SqlParameter("@soluongcon", SqlDbType.Int);
            soluongcon.Direction = ParameterDirection.Output;
            lstParameter.Add(soluongcon);
            try
            {
                return int.Parse(DataProvider.RunStoredProcParams_ReadOutPut("sp_soluongsp_left", db, lstParameter, "@soluongcon"));
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}
